package com.google.gwt.gwtpages.client.message;

import com.google.gwt.user.client.ui.Focusable;
import com.google.gwt.user.client.ui.HasWidgets;

/**
 * Interface used as the notification entity for GWT Pages. All instances are
 * used with {@link GWTMessages} for application control.
 * 
 * @author Joe Hudson
 */
public class GWTMessage {

	public static final int LEVEL_FATAL = 10000;
	public static final int LEVEL_ERROR = 20000;
	public static final int LEVEL_WARN = 30000;
	public static final int LEVEL_INFO = 40000;
	public static final int LEVEL_SUCCESS = 50000;

	private String message;
	private HasWidgets container;
	private Focusable component;
	private int level;

	public GWTMessage(String message, int level, Focusable component) {
		this.message = message;
		this.component = component;
		this.level = level;
	}

	/**
	 * Utility method to create a success message
	 * 
	 * @param message
	 *            the message text
	 */
	public static GWTMessage success(String message) {
		return new GWTMessage(message, LEVEL_SUCCESS, null);
	}

	/**
	 * Utility method to create an info message
	 * 
	 * @param message
	 *            the message text
	 */
	public static GWTMessage info(String message) {
		return new GWTMessage(message, LEVEL_INFO, null);
	}

	/**
	 * Utility method to create an info message
	 * 
	 * @param message
	 *            the message text
	 */
	public static GWTMessage error(String message) {
		return new GWTMessage(message, LEVEL_ERROR, null);
	}

	/**
	 * Utility method to create an error message
	 * 
	 * @param message
	 *            the message text
	 * @param component
	 *            the associated component or null if N/A
	 */
	public static GWTMessage error(String message, Focusable component) {
		return new GWTMessage(message, LEVEL_ERROR, component);
	}

	/**
	 * Utility method to create an error message
	 * 
	 * @param message
	 *            the message text
	 * @param component
	 *            the associated component or null if N/A
	 * @param viewContainer container for the message view
	 */
	public static GWTMessage error(String message, Focusable component, HasWidgets viewContainer) {
		return new GWTMessage(message, LEVEL_ERROR, component).setViewContainer(viewContainer);
	}

	/**
	 * Return the message text
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * Set the message text
	 * 
	 * @param message
	 *            the message text
	 */
	public GWTMessage setMessage(String message) {
		this.message = message;
		return this;
	}

	/**
	 * Return the component associated with the message
	 */
	public Focusable getComponent() {
		return component;
	}

	/**
	 * Set the associated component
	 * 
	 * @param component
	 *            the component
	 */
	public GWTMessage setComponent(Focusable component) {
		this.component = component;
		return this;
	}

	/**
	 * Return the message level.  See the LEVEL_* statics on this class
	 */
	public int getLevel() {
		return level;
	}

	/**
	 * Set the message level
	 * 
	 * @param level
	 *            the message level
	 */
	public GWTMessage setLevel(int level) {
		this.level = level;
		return this;
	}

	/**
	 * Return an optional container for the messages 
	 */
	public HasWidgets getViewContainer() {
		return container;
	}

	/**
	 * Set an optional view container for the messages
	 */
	public GWTMessage setViewContainer(HasWidgets container) {
		this.container = container;
		return this;
	}
}